package com.bee2c.pub.home.task;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.bee2c.pub.home.task.dao.PubTaskDao;
import com.bee2c.pub.home.task.dao.PubTaskTypeDao;
import com.bee2c.pub.home.task.entity.PubTask;
import com.bee2c.pub.home.task.entity.PubTaskType;
import com.bee2c.pub.util.bean.ProcessResult;

@Transactional
@Service
public class PubTaskServiceImpl implements PubTaskService {

	@Autowired
	private PubTaskDao taskDao;

	@Autowired
	private PubTaskTypeDao taskTypeDao;

	@Override
	public List<PubTask> findWaitToDoTask(String transferType, int size) {
		return taskDao.findWaitToDoTask(transferType, size);
	}

	@Override
	public ProcessResult updateTask(PubTask task) {
		task.setLastUpdateDate(new Date());
		taskDao.save(task);
		return ProcessResult.success();
	}

	@Override
	public Map<String, PubTaskType> findAllTaskType() {
		List<PubTaskType> taskTypes = taskTypeDao.findByEnabledFlag("Y");
		Map<String, PubTaskType> map = new HashMap<String, PubTaskType>();
		for (PubTaskType taskType : taskTypes) {
			map.put(taskType.getTaskTypeCode(), taskType);
		}
		return map;
	}

	@Override
	public ProcessResult insertTask(PubTask task) {
		task.setCreationDate(new Date());
		task.setLastUpdateDate(task.getCreationDate());
		taskDao.save(task);
		return ProcessResult.success();
	}
}
